<template>
  <div>
    <div class="userContent">
      <el-row
        v-loading="loading"
        style="width: 100%">
        <el-col :span="8">
          <el-form ref="form" :model="form" label-width="100px">
            <el-form-item prop="title" label="标题">
              <el-input v-model="form.title" disabled></el-input>
            </el-form-item>
            <el-form-item prop="tag_id" label="任务类别">
              <el-input v-model="form.tag_id" disabled></el-input>
            </el-form-item>
            <el-form-item prop="date" label="发布日期">
              <el-input v-model="form.date" disabled></el-input>
            </el-form-item>
            <el-form-item prop="content" label="任务内容">
              <el-input type="textarea" :autosize="{minRows:1,maxRows:50}" v-model="form.content" disabled></el-input>
            </el-form-item>
            <el-form-item prop="publisherName" label="发布者">
              <el-input v-model="form.publisherName" disabled></el-input>
            </el-form-item>
            <el-form-item prop="reward" label="酬金">
              <el-input v-model="form.reward" disabled></el-input>
            </el-form-item>
            <el-form-item prop="phone" label="电话号码">
              <el-input v-model="form.phone" disabled></el-input>
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="open">接受任务</el-button>
<!--          <el-button type="primary" @click="AcceptTask">接受任务</el-button>-->
          <el-button type="primary" @click="ToReportTask()">举报任务</el-button>
        </el-col>
      </el-row><br>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import router from '../../router'
// import Bus from 'bus.js'

export default {
  name: `ReadTask`,
  data () {
    return {
      id: this.$route.params.id,
      form: {},
      loading: false
    }
  },
  methods: {
    open () {
      this.$alert('确定要接受任务吗', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          this.AcceptTask()
        }
      })
    },
    ToReportTask () {
      return this.$router.push({
        path: '/ReportTask',
        name: 'ReportTask',
        params: {
          id: this.id.toString()
        }
      })
    },
    AcceptTask () {
      this.loading = true
      axios.get(`user/task/accept`, {
        params: {
          id: this.id.toString()
        }
      }).then(res => {
        if (res.data.status === 100) {
          this.loading = false
          alert(res.data.data.msg)
          return this.$router.push({
            path: '/TaskList'
          })
        } else {
          this.loading = false
          alert('操作失败')
          return this.$router.push({
            path: '/TaskList'
          })
        }
      }).catch(err => {
        this.loading = false
        alert(err)
        router.replace('/login')
      })
    },
    getTaskData (id) {
      if (id === null) {
        return this.$router.push({
          path: '/TaskList'
        })
      } else {
        axios.get(`user/task/Detail`, {
          params: {
            id: this.id
          }
        }).then(res => {
          if (res.data.status === 100) {
            this.form = res.data.data
          } else {
            alert('获取失败')
          }
        }).catch(err => {
          alert(err)
          router.replace('/login')
        })
      }
    }
  },
  created () {
    this.getTaskData(this.$route.params.id)
  }
// 每次激活时
//   activated () {
//     // 根据key名获取传递回来的参数，data就是map
//     Bus.$on('id', function (data) {
//       // 赋值给当前页面的值
//       this.id = data
//     }.bind(this))
//   }
}
</script>

<style scoped>
.userContent {
  margin: 0 auto;
}

.el-input.is-disabled .el-input__inner {
  background-color: #ffffff;
  border-color: #ffffff;
  color: #222;
  /*cursor: pointer;*/
}
.el-input.el-textarea {
  color: #222222;
}
.el-textarea.is-disabled .el-textarea__inner {
  background-color: #ffffff;
  border-color: #ffffff;
  color: #222;
  cursor: pointer;
}
</style>
